package com.lanou.service.Impl;

import com.lanou.entity.User;
import com.lanou.mapper.UserMapper;
import com.lanou.service.UserService;
import com.lanou.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author lizhenjiang
 * @Date 2025年08月12日 17:37
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int addUser(User user) {
        user.setPassword(MD5Util.encode(user.getAccount()+user.getPassword()));
        return userMapper.addUser(user);
    }

    @Override
    public int updateUser(User user) {
        String account;
        if (user.getAccount() == null || user.getAccount().isEmpty()) {
            account = userMapper.selectUser(user.getId()).getAccount();
        } else {
            account = user.getAccount();
        }

        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            user.setPassword(MD5Util.encode(account + user.getPassword()));
        } else {
            user.setPassword(null);
        }

        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.selectUser(id);
    }

    @Override
    public User login(String account, String password) {
        password=MD5Util.encode(account+password);
        return userMapper.userLogin(account, password);
    }

    @Override
    public Boolean findUserPhone(String phone) {
        return userMapper.findUserPhone(phone)>0;
    }


}
